<?php

class Ext_Publish_Attachment_Add_Adapter_Image implements Ext_Publish_Attachment_Add_Interface
{
    protected $_articleId;
    
    protected $_messager;
    const IMAGE_EXISTS              = 'imageExists';
    protected $_messageTemplates = array(
        self::IMAGE_EXISTS              => "Image already exists.",
    );
    
    public function __construct($articleId)
    {
        $this->_articleId = $articleId;
        $this->_messager  = new Ext_Messager($this->_messageTemplates);
    }
    
    public function add()
    {
        $_result = array(
            'code'      => Ext_Publish_Attachment_Add_Result::FAILURE,
            'datas'     => array(),
            'messages'  => array(),
        );
        
        $uploader = new Ext_Image_Upload();
        $uploadResulter = $uploader->upload();
        return "";
        if(!$uploadResulter->isValid())
        {
            $_result['messages'] = $uploadResulter->getMessages();
            return new Ext_Publish_Attachment_Add_Result($_result['code'], $_result['datas'], $_result['messages']);
        }
        $data = $uploadResulter->getData();
        $imageFile   = $data->file;
        $imageType   = $data->type;
        $imageSize   = $data->size;
        $description = $data->name;
        $htmlOn      = 0;
                
        $imageSaver = new Ext_Image_Save($imageFile);
        $imageSaveResulter = $imageSaver->save();
        if(!$imageSaveResulter->isValid())
        {
            $_result['messages'] = $imageSaveResulter->getMessages();
            return new Ext_Publish_Attachment_Add_Result($_result['code'], $_result['datas'], $_result['messages']);
        }
        $imageId = $imageSaveResulter->getImageId();
        
        $articleImage = App_Model_DbTable_Article_Attachment_Image::getInstance()->getRowByArticleId($this->_articleId);
        if($articleImage){
            $ids = $articleImage['ids'];

            $imageIdArray = array();
            $idsArray = explode(';', $ids);
            for($i=0;$i<count($idsArray);$i++)
            {
                $attachmentImageId = $idsArray[$i];
                if(!$attachmentImageId){
                    continue;
                }

                $attachmentImage = App_Model_DbTable_Attachment_Image::getInstance()->getRowById($attachmentImageId);
                if(!$attachmentImage){
                    continue;
                }
                $imageIdArray[] = $attachmentImage['imageId'];
            }
                    
            if(in_array($imageId, $imageIdArray)){
                 $_result['messages'][] = $this->_messager->addMessage(self::IMAGE_EXISTS);
                 return new Ext_Publish_Attachment_Add_Result($_result['code'], $_result['datas'], $_result['messages']);
            }
        }
        
        $row = array(
            'articleId'     => $this->_articleId,
            'imageId'       => $imageId,
            'description'   => $description,
            'htmlOn'        => $htmlOn,
        );
        $attachmentImageId = App_Model_DbTable_Attachment_Image::getInstance()->addNew($row);
        
        $imager = new Ext_Article_Attachment_Image($this->_articleId);
        $imager->add($attachmentImageId);
        
        $article = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$article){
            throw new Ext_Publish_Attachment_Add_Exception("Article record not found.");
        }
        
        $attachmentsArray = unserialize($article['attachments']);
        if(!is_array($attachmentsArray)){
            $attachmentsArray = array();
        }
        $attachment = array("type"=>'image', "id"=>$attachmentImageId);
        array_unshift($attachmentsArray, $attachment);
        if($attachmentsArray){
            $row = array(
                'attachments'  => serialize($attachmentsArray),
            );
            App_Model_DbTable_Article::getInstance()->addOld($row, $this->_articleId);
        }
        
        $_result['code']                       = Ext_Publish_Attachment_Add_Result::SUCCESS;
        $_result['datas']['attachmentImageId'] = $attachmentImageId;
        return new Ext_Publish_Attachment_Add_Result($_result['code'], $_result['datas'], $_result['messages']);
    }
}
